// components/selectAddress/selectAddress.js
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
var app = getApp();
var range = 792 / (750 / app.data.deviceInfo.windowWidth);

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    
  },

  /**
   * 组件的初始数据
   */
  data: {
    flag: true,
    current: 0,
    areaSelectedStr: '请选择省市区',
    provinceName: '请选择',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //展示弹框
    _showPopup () {
      var that = this;
      this.triggerEvent('disabledarea', 1);
      shihan.sendRequest(config.giveregion + '0', 'GET', {})
        .then(function (res) { //获取行政地区
          that.setData({
            province: res.data.data.sh_items,
            provinceObjects: res.data.data.sh_items
          })
        }, function (error) {
          console.log(error);
        });
      var animation = wx.createAnimation({
        duration: 500,
        timingFunction: 'ease-in-out',
      });
      this.animation = animation;
      animation.translateY(-range).step();
      this.setData({
        animationData: this.animation.export(),
        flag: !this.data.flag
      });
    },
    //隐藏弹框
    hidePopup () {
      this.triggerEvent('disabledarea', 0);
      this.animation.translateY(range).step();
      this.setData({
        animationData: this.animation.export(),
        flag: !this.data.flag
      });     
    },
    changeCurrent (e) {
      // 记录点击的标题所在的区级级别
      var current = e.currentTarget.dataset.current;
      this.setData({
        current: current
      });
    },
    currentChanged (e) {
      this.setData({
        current: e.detail.current
      })
    },
    provinceTapped (e) {
      // 标识当前点击省份，记录其名称与主键id都依赖它
      var index = e.currentTarget.dataset.index;
      var id = e.currentTarget.dataset.id;
      // current为1，使得页面向左滑动一页至市级列表
      // provinceIndex是市区数据的标识
      this.setData({
        provinceName: this.data.province[index].sh_name,
        regionName: '',
        townName: '',
        provinceIndex: index,
        cityIndex: -1,
        regionIndex: -1,
        townIndex: -1,
        region: [],
        town: []
      });
      var that = this;
      shihan.sendRequest(config.giveregion + id, 'GET', {})
        .then(function (res) { //获取行政地区
          that.setData({
            cityName: '请选择',
            city: res.data.data.sh_items,
            cityObjects: res.data.data.sh_items
          })
          that.setData({
            current: 1
          })
        }, function (error) {
          console.log(error);
        });
    },
    cityTapped (e) {
      // 标识当前点击县级，记录其名称与主键id都依赖它
      var index = e.currentTarget.dataset.index;
      var id = e.currentTarget.dataset.id;
      // current为1，使得页面向左滑动一页至市级列表
      // cityIndex是市区数据的标识
      this.setData({
        cityIndex: index,
        regionIndex: -1,
        townIndex: -1,
        cityName: this.data.city[index].sh_name,
        regionName: '',
        townName: '',
        town: []
      });
      var that = this;
      shihan.sendRequest(config.giveregion + id, 'GET', {})
        .then(function (res) { //获取行政地区
          that.setData({
            regionName: '请选择',
            region: res.data.data.sh_items,
            regionObjects: res.data.data.sh_items
          })
          that.setData({
            current: 2
          })
        }, function (error) {
          console.log(error);
        });
    },
    regionTapped (e) {
      // 标识当前点击镇级，记录其名称与主键id都依赖它
      var index = e.currentTarget.dataset.index;
      var id = e.currentTarget.dataset.id;
      // current为1，使得页面向左滑动一页至市级列表
      // regionIndex是县级数据的标识
      this.setData({
        regionIndex: index,
        regionName: this.data.region[index].sh_name
      });
      var areaSelectedStr = this.data.provinceName + this.data.cityName + this.data.regionName;
      this.setData({
        areaSelectedStr: areaSelectedStr
      });
      this.hidePopup();
      var myEventDetail = {
        address: this.data.areaSelectedStr,
        provinceName: this.data.provinceName,
        province_id: this.data.province[this.data.provinceIndex].sh_id,
        cityName: this.data.cityName,
        city_id: this.data.city[this.data.cityIndex].sh_id,
        regionName: this.data.regionName,
        region_id: this.data.region[this.data.regionIndex].sh_id 
      } // detail对象，提供给事件监听函数
      this.triggerEvent('changeaddress', myEventDetail);
    }
  }
})